package com.jiayang.hhotel.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.BookDinnerSearchAdapter;
import com.jiayang.hhotel.bean.BookDinnerSearchModel;
import com.jiayang.hhotel.bean.ReservationSearchModel;
import com.jiayang.hhotel.presenter.BookDinnerSearchPresenter;
import com.jiayang.hhotel.util.LogUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;

import cn.droidlover.xdroidmvp.mvp.XLazyFragment;

/**
 * Created by Administrator on 2017/5/23 0023.
 * 订餐查询子fragment 传递参数用于判断到底是来自那种item的请求
 */

public class BookDinnerSearchFragment extends XLazyFragment<BookDinnerSearchPresenter> {

    private SwipeRefreshLayout fresh;
    private SwipeMenuRecyclerView recyclerView;
    private int flag;

    private ArrayList<BookDinnerSearchModel> list;

    private BookDinnerSearchAdapter adapter;

    public BookDinnerSearchFragment(int flag){
        this.flag=flag;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        fresh= (SwipeRefreshLayout) getRootView().findViewById(R.id.fresh);
        recyclerView= (SwipeMenuRecyclerView) getRootView().findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        list=new ArrayList<>();
        list.add(new BookDinnerSearchModel("11","11","11","11","11","11","8888-88-88"));
        list.add(new BookDinnerSearchModel("11","11","11","11","11","11","8888-88-88"));
        list.add(new BookDinnerSearchModel("11","11","11","11","11","11","8888-88-88"));
        list.add(new BookDinnerSearchModel("11","11","11","11","11","11","8888-88-88"));
        list.add(new BookDinnerSearchModel("11","11","11","11","11","11","8888-88-88"));
        list.add(new BookDinnerSearchModel("11","11","11","11","11","11","8888-88-88"));

        isRefresh(true);
        getP().asyncLoad();
        fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LogUtil.showLog("onRefresh");
                getP().asyncLoad();
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if(!recyclerView.canScrollVertically(1)){

                }
            }
        });
    }

    private void isRefresh(final boolean is){
        fresh.post(new Runnable() {
            @Override
            public void run() {
                fresh.setRefreshing(is);
            }
        });
    }

    public void setAdapter(ReservationSearchModel model){
        isRefresh(false);
        if(adapter==null){
            adapter=new BookDinnerSearchAdapter(getActivity(),list);
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_reservation_search_item;
    }

    @Override
    public BookDinnerSearchPresenter newP() {
        return new BookDinnerSearchPresenter();
    }
}
